#The importance of encoding
import sys
sys.getdefaultencoding()


#Opening a file for writing
#Make sure you edit the filename to include the location YOU are using for your files
filename = 'C:\\Users\\mtthw\\Desktop\\pyfund\\wasteland.txt'

f  =  open(filename,  mode='wt',  encoding='utf-8')


#Writing to files
help(f)
f.write('What  are  the  roots  that  clutch,  ')
f.write('what  branches  grow\n')
f.write('Out  of  this  stony  rubbish?  ')


#Closing files
f.close()


#Reading files
g  =  open(filename,  mode='rt',  encoding='utf-8')
g.read(32)
g.read()
g.read()

g.seek(0)
g.readline()
g.readline()
g.readline()


#Reading multiple lines at once
g.seek(0)
g.readlines()
g.close()


#Appending to files
h  =  open(filename,  mode='at',  encoding='utf-8')
h.writelines(
    ['Son of man,\n',
    'You cannot say, or guess, ',
    'for you know only,\n',
    'A heap of broken images, ',
    'where the sun beats\n'])
h.close()
